﻿#include "dxstdafx.h"
#include "GuiProvince.h"
#include "Data\ProgramData\ProvinceData.h"
#include "Data\ScriptData\LandedTitles\LandedTitles.h"
#include "Data\ProgramData\LandedTitlesData.h"

void GuiProvince::SetChooseProvincePtr(_stProvinceInfo* p)
{
	if (nullptr == p || !p->IsCountyPalatine())
	{
		return;
	}
	m_pProvinceChoose = p;
	if (GetVisible())
	{
		RefreshUI();
	}
	else
	{
		SetVisible(true);
	}
}

void GuiProvince::OnShow()
{
	RefreshUI();
	return CDXUTDialog::OnShow();
}

void GuiProvince::RefreshUI()
{
	// 这些指针没法在构造函数中赋值
	if (nullptr == m_pStatic_CountName)
	{
		m_pStatic_CountName = static_cast<CDXUTStatic*>(GetControl(Static_Province_CountName));
		CHECK_RETURN(m_pStatic_CountName);
	}
	if (nullptr == m_pStatic_DeJureDuke)
	{
		m_pStatic_DeJureDuke = static_cast<CDXUTStatic*>(GetControl(Static_Province_DeJureDuke));
		CHECK_RETURN(m_pStatic_DeJureDuke);
	}
	if (nullptr == m_pStatic_DeJureKing)
	{
		m_pStatic_DeJureKing = static_cast<CDXUTStatic*>(GetControl(Static_Province_DeJureKing));
		CHECK_RETURN(m_pStatic_DeJureKing);
	}
	if (nullptr == m_pStatic_CultrueValue)
	{
		m_pStatic_CultrueValue = static_cast<CDXUTStatic*>(GetControl(Static_Province_CultrueValue));
		CHECK_RETURN(m_pStatic_CultrueValue);
	}
	if (nullptr == m_pStatic_ReligionValue)
	{
		m_pStatic_ReligionValue = static_cast<CDXUTStatic*>(GetControl(Static_Province_ReligionValue));
		CHECK_RETURN(m_pStatic_ReligionValue);
	}

	if (nullptr == m_pProvinceChoose && !m_pProvinceChoose->IsCountyPalatine())
	{
		return;
	}
		
	m_pStatic_CountName->SetText(m_pProvinceChoose->GetDisplayName().c_str());
	
	_stLandedTitlesData* pDeJureDuke = m_pProvinceChoose->pLandedTitlesData->m_pDeJureFather;
	if (pDeJureDuke && pDeJureDuke->IsAvailable())
	{
		m_pStatic_DeJureDuke->SetText(pDeJureDuke->GetDisplayName().c_str());
	}
	_stLandedTitlesData* pDeJureKing = pDeJureDuke->m_pDeJureFather;
	if (pDeJureKing && pDeJureKing->IsAvailable())
	{
		m_pStatic_DeJureKing->SetText(pDeJureKing->GetDisplayName().c_str());
	}
}

void GuiProvince::OnEvent(UINT nEvent, int nControlID, CDXUTControl* pControl)
{
	switch (nEvent)
	{
	case EVENT_BUTTON_CLICKED:
	{
		switch (nControlID)
		{
		case Button_Province_Cancel:
		{
			SetVisible(false);
			if (m_pProvinceChoose)
			{
				m_pProvinceChoose->SetSelect(false);
			}
			m_pProvinceChoose = nullptr;
			break;
		}
		default:
			break;
		}
		break;
	}
	default:
		break;
	}
}
